#!/bin/bash
#
# Kubernetes 监控栈部署脚本
# 使用：./deploy.sh 或 kubectl apply -f deploy-kubernetes/
#

set -e

NAMESPACE="monitoring"
DEPLOY_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "🚀 开始部署 Prometheus 监控栈..."
echo ""
echo "📦 部署目录: ${DEPLOY_DIR}"
echo "📦 命名空间: ${NAMESPACE}"
echo ""

# 统一部署所有 YAML 文件（按文件名自动排序）
echo "✅ 执行部署命令..."
kubectl apply -f "${DEPLOY_DIR}/"

echo ""
echo "⏳ 等待核心服务就绪..."
kubectl wait --for=condition=ready pod -l app=prometheus -n ${NAMESPACE} --timeout=120s 2>/dev/null || echo "⚠️  Prometheus 启动中..."
kubectl wait --for=condition=ready pod -l app=grafana -n ${NAMESPACE} --timeout=120s 2>/dev/null || echo "⚠️  Grafana 启动中..."

echo ""
echo "✅ 部署完成！"
echo ""
echo "📋 部署状态："
kubectl get pods -n ${NAMESPACE} -o wide

echo ""
echo "🌐 访问地址："
echo "  Prometheus:   http://节点IP:30909"
echo "  Alertmanager: http://节点IP:30930"
echo "  Grafana:      http://节点IP:30300 (admin/admin)"
echo ""
echo "🔍 查看详细信息："
echo "  kubectl get all -n ${NAMESPACE}"
echo "  kubectl logs -n ${NAMESPACE} -l app=prometheus"
